package org.phantom.securityframework.web.filter;

import org.apache.shiro.config.Ini;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.util.CollectionUtils;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.phantom.securityframework.auth.DefaultHttpAuthInitFilter;
import org.phantom.securityframework.web.WebSecurityManagerFactory;

import org.phantom.securityframework.web.mgt.SessionManager;
/**
 * web应用初始化Shiro环境Filter，将Shiro配置项注入到Shiro中
 * @author david
 *
 */
public class WebAuthInitFilter extends DefaultHttpAuthInitFilter {

    public static final String CONFIG_INIT_PARAM_NAME = "config";
	/**
	 * 获取web.xml中的Shiro配置
	 */
	@Override
	protected String getShiroConfig() {
        String config = getInitParam(CONFIG_INIT_PARAM_NAME);
        if (config != null) {
        	return config;
        }
        return null;
	}

	/**
	 * 覆盖默认SessionManager
	 */
	@Override
	protected org.apache.shiro.session.mgt.SessionManager getSessionManager() {
		return new SessionManager();
	}


	/**
	 * 覆盖默认IniSecurityManagerFactory，创建securityframework自定义WebSecurityManagerFactory
	 * @see org.phantom.securityframework.web.WebSecurityManagerFactory
	 */
    protected IniSecurityManagerFactory getSecurityManagerFactory(Ini ini){
        if (CollectionUtils.isEmpty(ini)) {
            return new WebSecurityManagerFactory();
        } else {
        	return new WebSecurityManagerFactory(ini);
        }
    }

	@Override
	protected DefaultWebSecurityManager getSecurityManager(IniSecurityManagerFactory factory) {
		DefaultWebSecurityManager securityManager = (DefaultWebSecurityManager) factory.getInstance();
		return securityManager;
	}

}
